package com.example.studentapp.course;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.studentapp.R;
import com.example.studentapp.adapters.CourseListAdapter;
import com.example.studentapp.beans.CourseList;
import com.example.studentapp.db.SQLiteDB;

import java.util.ArrayList;
import java.util.List;

public class LookActivity extends AppCompatActivity {
    ImageView course_returnIV;
    TextView course_lookTV;
    ListView course_lookLV;
    List<CourseList> list;
    CourseListAdapter courseListAdapter;
    SQLiteDB sqLiteDB;
    SQLiteDatabase sqLiteDatabase;



    @Override
    @SuppressLint("Range")
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_look);
        course_returnIV = findViewById(R.id.course_returnIV);
        course_lookTV = findViewById(R.id.course_lookTV);
        course_lookLV = findViewById(R.id.course_lookLV);

        list = new ArrayList<>();

        // 数据的获取
        sqLiteDB = new SQLiteDB(getApplicationContext());
        sqLiteDatabase = sqLiteDB.getWritableDatabase();
        String sql = "select * from course_list";
        Cursor cursor = sqLiteDatabase.rawQuery(sql,null);
        if(cursor.getCount() >0){
            cursor.moveToFirst();
            for(int i=0; i < cursor.getCount();i++){
                String name = cursor.getString(cursor.getColumnIndex("name"));
                int id = cursor.getInt(cursor.getColumnIndex("id"));
                list.add(new CourseList(name, id));
                cursor.moveToNext();
            }
            // 设配器
            courseListAdapter = new CourseListAdapter(getApplicationContext(),list);
            course_lookLV.setAdapter(courseListAdapter);
            course_lookTV.setVisibility(View.GONE);
            course_lookLV.setVisibility(View.VISIBLE);
        }else{
            course_lookTV.setVisibility(View.VISIBLE);
            course_lookLV.setVisibility(View.GONE);
        }

        // 列表点击事件
        course_lookLV.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Dialog myDialog = new Dialog(LookActivity.this);
                myDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                Window window = myDialog.getWindow();
                window.setContentView(R.layout.del_course_dialog);
                myDialog.show();
                Button course_okBtn = window.findViewById(R.id.course_del_okBtn);
                Button course_cancelBtn = window.findViewById(R.id.course_del_cancelBtn);

                course_cancelBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        myDialog.dismiss();
                    }
                });
                course_okBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        try {
                            String del_sql = "delete from course_list where id=" + list.get(i).getId();
                            sqLiteDatabase.execSQL(del_sql);
                            list.remove(i);
                            courseListAdapter.notifyDataSetChanged();
                            myDialog.dismiss();
                            Toast.makeText(getApplicationContext(), getString(R.string.course_del_ok), Toast.LENGTH_SHORT).show();

                        } catch (Exception e) {
                            Toast.makeText(getApplicationContext(), getString(R.string.course_del_failed), Toast.LENGTH_SHORT).show();
                        }

                    }
                });

            }
        });
        course_returnIV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }
}